<html>

<head>

<?php

include "../includes/constants.php";

?>

<script src="../includes/functions.js"></script>

<title>EQEmu - Item Search</title>

</head>

<body>

<?php
/*
<?php OutputSearchNum("","");?>
*/

function OutputSearchNum($FieldName,$DisplayName,$InputSize="") {
	echo "\t\t\t" . $DisplayName . ":
			<select name=\"" . $FieldName . "_range\">
				<option value=\"=\"";
	if ($_GET["{$FieldName}_range"] == "=") {echo " selected";};
	echo ">=</option>
				<option value=\">=\"";
	if ($_GET["{$FieldName}_range"] == ">=") {echo " selected";};
	echo ">>=</option>
				<option value=\"<=\"";
	if ($_GET["{$FieldName}_range"] == "<=") {echo " selected";};
	echo "><=</option>
			</select><input type=\"text\" size=\"" . $InputSize . "\" name=\"" . $FieldName . "\" value=\"" . $_GET[$FieldName] . "\">\n";
};

?>

<form method="GET" action="">
<table border="0" cellpadding="0">
	<tr>
		<td>
<?php OutputSearchNum("id","id",1);?>
<?php OutputSearchNum("minstatus","minstatus",1);?>
		</td>
	</tr>
	<tr>
		<td>
			Name:
			<select name="Name_range">
				<option value="Contains"<?php if ($_GET["Name_range"] == "Contains") {echo " selected";};?>>Contains</option>
				<option value="Begins"<?php if ($_GET["Name_range"] == "Begins") {echo " selected";};?>>Begins</option>
				<option value="Ends"<?php if ($_GET["Name_range"] == "Ends") {echo " selected";};?>>Ends</option>
				<option value="Matches"<?php if ($_GET["Name_range"] == "Matches") {echo " selected";};?>>Matches</option>
			</select><input type="text" size="" name="Name" value="<?php echo $_GET["Name"];?>">
		</td>
	</tr>
	<tr>
		<td>
			MAGIC ITEM:
			<select name="magic">
				<option></option>
				<option value="1"<?php if ($_GET["magic"] == "1") {echo " selected";};?>>Yes</option>
				<option value="0"<?php if ($_GET["magic"] == "0") {echo " selected";};?>>No</option>
			</select><input type="hidden" name="magic_range" value="=">
			LORE:
			<select name="loregroup_range">
				<option></option>
				<option value="!="<?php if ($_GET["loregroup_range"] == "!=") {echo " selected";};?>>Yes</option>
				<option value="="<?php if ($_GET["loregroup_range"] == "=") {echo " selected";};?>>No</option>
			</select><input type="hidden" name="loregroup" value="0">
<!--			NODROP:
			TEMPORARY:
			ARTIFACT:
-->		</td>
	</tr>
	<tr>
		<td>
			Slot:
			<select name="slots">
				<option></option>
<?php

foreach ($Slots as $key => $value) {
	echo "\t\t\t\t<option value=\"" . $key . "\"";
	if ($_GET["slots"] == $key) {echo " selected";};
	echo ">" . $value . "</option>\n";
};

?>
			</select>
			<input type="hidden" name="slots_range" value="&">
		</td>
	</tr>
	<tr>
		<td>
<!--		Skill:
-->
<?php OutputSearchNum("delay","Atk Delay",1);?>
		</td>
	</tr>
	<tr>
		<td>
<?php OutputSearchNum("damage","DMG",1);?>
<?php OutputSearchNum("ac","AC",2);?>
		</td>
	</tr>
	<tr>
		<td>
<?php OutputSearchNum("astr","STR",2);?>
<?php OutputSearchNum("adex","DEX",1);?>
<?php OutputSearchNum("asta","STA",1);?>
<?php OutputSearchNum("acha","CHA",1);?>
<?php OutputSearchNum("awis","WIS",1);?>
<?php OutputSearchNum("aint","INT",1);?>
<?php OutputSearchNum("aagi","AGI",1);?>
<?php OutputSearchNum("hp","HP",1);?>
<?php OutputSearchNum("mana","MANA",1);?>
<?php OutputSearchNum("endur","Endurance",1);?>
		</td>
	</tr>
	<tr>
		<td>
<?php OutputSearchNum("fr","SV FIRE",1);?>
<?php OutputSearchNum("dr","SV DISEASE",1);?>
<?php OutputSearchNum("cr","SV COLD",1);?>
<?php OutputSearchNum("mr","SV MAGIC",1);?>
<?php OutputSearchNum("pr","SV POISON",1);?>
		</td>
	</tr>
	<tr>
		<td>
<?php OutputSearchNum("attack","Attack",1);?>
		</td>
	</tr>
	<tr>
		<td>
<?php OutputSearchNum("regen","Regeneration",1);?>
		</td>
	</tr>
	<tr>
		<td>
<?php OutputSearchNum("manaregen","Mana Regeneration",1);?>
		</td>
	</tr>
	<tr>
		<td>
<?php OutputSearchNum("enduranceregen","Endurance Regeneration",1);?>
		</td>
	</tr>
	<tr>
		<td>
<?php OutputSearchNum("damageshield","Damage Shield",1);?>
		</td>
	</tr>
	<tr>
		<td>
<?php OutputSearchNum("combateffects","Combat Effects",1);?>
<?php OutputSearchNum("shielding","Shielding",1);?>
<?php OutputSearchNum("avoidance","Avoidance",1);?>
		</td>
	</tr>
	<tr>
		<td>
<?php OutputSearchNum("accuracy","Accuracy",1);?>
		</td>
	</tr>
	<tr>
		<td>
<?php OutputSearchNum("haste","Haste",1);?>
		</td>
	</tr>
	<tr>
		<td>
<?php OutputSearchNum("reclevel","Recommended Level",1);?>
<?php OutputSearchNum("reqlevel","Required Level",1);?>
		</td>
	</tr>
	<tr>
		<td>
<?php OutputSearchNum("weight","WT",1);?>
<?php OutputSearchNum("range","Range",1);?>
			Size:
<?php OutputSearchNum("bagwr","Weight Reduction",1);?>
		</td>
	</tr>
	<tr>
		<td>
<?php OutputSearchNum("bagslots","Capacity");?>
<?php OutputSearchNum("bagsize","Size Capacity");?>
		</td>
	</tr>
	<tr>
		<td>
			Class:
			<select name="classes">
				<option></option>
				<option value="1"<?php if ($_GET["classes"] == "1") {echo " selected";};?>>Warrior</option>
				<option value="2"<?php if ($_GET["classes"] == "2") {echo " selected";};?>>Cleric</option>
				<option value="4"<?php if ($_GET["classes"] == "4") {echo " selected";};?>>Paladin</option>
				<option value="8"<?php if ($_GET["classes"] == "8") {echo " selected";};?>>Ranger</option>
				<option value="16"<?php if ($_GET["classes"] == "16") {echo " selected";};?>>Shadow Knight</option>
				<option value="32"<?php if ($_GET["classes"] == "32") {echo " selected";};?>>Druid</option>
				<option value="64"<?php if ($_GET["classes"] == "64") {echo " selected";};?>>Monk</option>
				<option value="128"<?php if ($_GET["classes"] == "128") {echo " selected";};?>>Bard</option>
				<option value="256"<?php if ($_GET["classes"] == "256") {echo " selected";};?>>Rogue</option>
				<option value="512"<?php if ($_GET["classes"] == "512") {echo " selected";};?>>Shaman</option>
				<option value="1024"<?php if ($_GET["classes"] == "1024") {echo " selected";};?>>Necromancer</option>
				<option value="2048"<?php if ($_GET["classes"] == "2048") {echo " selected";};?>>Wizard</option>
				<option value="4096"<?php if ($_GET["classes"] == "4096") {echo " selected";};?>>Magician</option>
				<option value="8192"<?php if ($_GET["classes"] == "8192") {echo " selected";};?>>Enchanter</option>
				<option value="16384"<?php if ($_GET["classes"] == "16384") {echo " selected";};?>>Beastlord</option>
				<option value="32768"<?php if ($_GET["classes"] == "32768") {echo " selected";};?>>Berserker</option>
			</select>
			<input type="hidden" name="classes_range" value="&">
		</td>
	</tr>
	<tr>
		<td>
			Race:
			<select name="races">
				<option></option>
				<option value="1"<?php if ($_GET["races"] == "1") {echo " selected";};?>>Human</option>
				<option value="2"<?php if ($_GET["races"] == "2") {echo " selected";};?>>Barbarian</option>
				<option value="4"<?php if ($_GET["races"] == "4") {echo " selected";};?>>Erudite</option>
				<option value="8"<?php if ($_GET["races"] == "8") {echo " selected";};?>>ELF</option>
				<option value="16"<?php if ($_GET["races"] == "16") {echo " selected";};?>>HIE</option>
				<option value="32"<?php if ($_GET["races"] == "32") {echo " selected";};?>>Dark Elf</option>
				<option value="64"<?php if ($_GET["races"] == "64") {echo " selected";};?>>HEF</option>
				<option value="128"<?php if ($_GET["races"] == "128") {echo " selected";};?>>Dwarf</option>
				<option value="256"<?php if ($_GET["races"] == "256") {echo " selected";};?>>Troll</option>
				<option value="512"<?php if ($_GET["races"] == "512") {echo " selected";};?>>Ogre</option>
				<option value="1024"<?php if ($_GET["races"] == "1024") {echo " selected";};?>>Halfling</option>
				<option value="2048"<?php if ($_GET["races"] == "2048") {echo " selected";};?>>Gnome</option>
				<option value="4096"<?php if ($_GET["races"] == "4096") {echo " selected";};?>>Iksar</option>
				<option value="8192"<?php if ($_GET["races"] == "8192") {echo " selected";};?>>Vah Shir</option>
				<option value="16384"<?php if ($_GET["races"] == "16384") {echo " selected";};?>>Froglok</option>
				<option value="32768"<?php if ($_GET["races"] == "32768") {echo " selected";};?>>Shroud</option>
			</select>
			<input type="hidden" name="races_range" value="&">
		</td>
	</tr>
<?php

$manual_fields = 3;

for ($y = 1; $y <= $manual_fields; $y++) {
	echo "\t<tr>
		<td>
			Field:
			<input type=\"text\" size=\"7\" name=\"manual" . $y . "_field\" value=\"" . $_GET["manual{$y}_field"] . "\">
			Range:
			<input type=\"text\" size=\"1\" name=\"manual" . $y . "_range\" value=\"" . $_GET["manual{$y}_range"] . "\">
			Value:
			<input type=\"text\" size=\"\" name=\"manual" . $y . "_value\" value=\"" . $_GET["manual{$y}_value"] . "\">
		</td>
	</tr>\n";
};


?>
	<tr>
		<td>
			Per Page:
			<input type="text" size="1" name="limit" value="<?php if ($_GET["limit"]) {echo $_GET["limit"];} else {echo "10";};?>">
			Offset:
			<input type="text" size="1" name="offset" value="<?php if ($_GET["offset"]) {echo $_GET["offset"];} else {echo "0";};?>">
		</td>
	</tr>
	<tr>
		<td>
			Order By:<br>
<?php

$orderby_num = 1;
for ($y = 1; $y <= $orderby_num; $y++) {
	echo "\t\t\t" . $y . ")
			<select name=\"sort" . $y . "\">
				<option></option>
				<option value=\"id\""; if ($_GET["sort{$y}"] == "id") {echo " selected";}; echo ">id</option>
				<option value=\"minstatus\""; if ($_GET["sort{$y}"] == "minstatus") {echo " selected";}; echo ">minstatus</option>
				<option value=\"Name\""; if ($_GET["sort{$y}"] == "Name") {echo " selected";}; echo ">Name</option>
				<option value=\"delay\""; if ($_GET["sort{$y}"] == "delay") {echo " selected";}; echo ">Atk Delay</option>
				<option value=\"damage\""; if ($_GET["sort{$y}"] == "damage") {echo " selected";}; echo ">DMG</option>
				<option value=\"ac\""; if ($_GET["sort{$y}"] == "ac") {echo " selected";}; echo ">AC</option>
				<option value=\"astr\""; if ($_GET["sort{$y}"] == "astr") {echo " selected";}; echo ">STR</option>
				<option value=\"adex\""; if ($_GET["sort{$y}"] == "adex") {echo " selected";}; echo ">DEX</option>
				<option value=\"asta\""; if ($_GET["sort{$y}"] == "asta") {echo " selected";}; echo ">STA</option>
				<option value=\"acha\""; if ($_GET["sort{$y}"] == "acha") {echo " selected";}; echo ">CHA</option>
				<option value=\"awis\""; if ($_GET["sort{$y}"] == "awis") {echo " selected";}; echo ">WIS</option>
				<option value=\"aint\""; if ($_GET["sort{$y}"] == "aint") {echo " selected";}; echo ">INT</option>
				<option value=\"aagi\""; if ($_GET["sort{$y}"] == "aagi") {echo " selected";}; echo ">AGI</option>
				<option value=\"hp\""; if ($_GET["sort{$y}"] == "hp") {echo " selected";}; echo ">HP</option>
				<option value=\"mana\""; if ($_GET["sort{$y}"] == "mana") {echo " selected";}; echo ">MANA</option>
				<option value=\"endur\""; if ($_GET["sort{$y}"] == "endur") {echo " selected";}; echo ">Endurance</option>
				<option value=\"regen\""; if ($_GET["sort{$y}"] == "regen") {echo " selected";}; echo ">HP Regen</option>
				<option value=\"manaregen\""; if ($_GET["sort{$y}"] == "manaregen") {echo " selected";}; echo ">Mana Regen</option>
				<option value=\"enduranceregen\""; if ($_GET["sort{$y}"] == "enduranceregen") {echo " selected";}; echo ">Endur Regen</option>
				<option value=\"reclevel\""; if ($_GET["sort{$y}"] == "reclevel") {echo " selected";}; echo ">Rec Level</option>
				<option value=\"reqlevel\""; if ($_GET["sort{$y}"] == "reqlevel") {echo " selected";}; echo ">Req Level</option>
			</select>
			<select name=\"sortdir" . $y . "\">
				<option value=\"DESC\""; if ($_GET["sortdir{$y}"] == "DESC") {echo " selected";}; echo ">DESC</option>
				<option value=\"ASC\""; if ($_GET["sortdir{$y}"] == "ASC") {echo " selected";}; echo ">ASC</option>
			</select><br>\n";
};

?>
		</td>
	</tr>
	<tr>
		<td>
			<button type="submit" name="submit">Search</button>
			<button type="reset">Clear</button>
			<a href="<?php echo $_SERVER["PHP_SELF"];?>">Start Over</a>
		</td>
	</tr>
</table>
</form>

<?php

if (isset($_GET["submit"])) {
	function Add2QueryNum($fieldname) {
		if ((isset($_GET["{$fieldname}"]) && $_GET["{$fieldname}"]!= "") && (isset($_GET["{$fieldname}_range"]) && $_GET["{$fieldname}_range"] != "")) {
			global $query;
			global $x;
			if ($x > 0) {
				$query .= " AND ";
			} else {
				$query .= " WHERE ";
			};
			$query .= " " . $fieldname . $_GET["{$fieldname}_range"] . "'" . $_GET["{$fieldname}"] . "' ";
			$x++;
		};
	};
	function Add2QueryChr($fieldname) {
		if ((isset($_GET["{$fieldname}"]) && $_GET["{$fieldname}"] != "") && (isset($_GET["{$fieldname}_range"]) && $_GET["{$fieldname}_range"] != "")) {
			global $query;
			global $x;
			if ($x > "0") {
				$query .= " AND ";
			} else {
				$query .= " WHERE ";
			};
			if ($_GET["{$fieldname}_range"] == "Contains") {
				$like_beginning = "%";
				$like_end = "%";
			} elseif ($_GET["{$fieldname}_range"] == "Begins") {
				$like_beginning = "";
				$like_end = "%";
			} elseif ($_GET["{$fieldname}_range"] == "Ends") {
				$like_beginning = "%";
				$like_end = "";
			} elseif ($_GET["{$fieldname}_range"] == "Matches") {
				$like_beginning = "";
				$like_end = "";
			};
			$query .= " " . $fieldname . " LIKE '" . $like_beginning . $_GET["{$fieldname}"] . $like_end . "' ";
			$x++;
		};
	};
	$x = 0;
	$query = "SELECT id,Name FROM items ";
	Add2QueryNum(id);
	Add2QueryNum(minstatus);
	Add2QueryChr(Name);
	Add2QueryNum(magic);
	Add2QueryNum(loregroup);
	Add2QueryNum(slots);
	Add2QueryNum(delay);
	Add2QueryNum(damage);
	Add2QueryNum(ac);
	Add2QueryNum(astr);
	Add2QueryNum(adex);
	Add2QueryNum(asta);
	Add2QueryNum(acha);
	Add2QueryNum(awis);
	Add2QueryNum(aint);
	Add2QueryNum(aagi);
	Add2QueryNum(hp);
	Add2QueryNum(mana);
	Add2QueryNum(endur);
	Add2QueryNum(fr);
	Add2QueryNum(dr);
	Add2QueryNum(cr);
	Add2QueryNum(mr);
	Add2QueryNum(pr);
	Add2QueryNum(attack);
	Add2QueryNum(regen);
	Add2QueryNum(manaregen);
	Add2QueryNum(enduranceregen);
	Add2QueryNum(damageshield);
	Add2QueryNum(combateffects);
	Add2QueryNum(shielding);
	Add2QueryNum(avoidance);
	Add2QueryNum(accuracy);
	Add2QueryNum(haste);
	Add2QueryNum(reclevel);
	Add2QueryNum(reqlevel);
	Add2QueryNum(classes);
	Add2QueryNum(races);
	
	// Add in manual fields to query
	for ($y = 0; $y <= $manual_fields; $y++) {
		if ((isset($_GET["manual{$y}_field"]) && $_GET["manual{$y}_field"] != "") && (isset($_GET["manual{$y}_range"]) && $_GET["manual{$y}_range"] != "")) {
			global $query;
			global $db;
			global $x;
			if ($x > "0") {
				$query .= " AND ";
			} else {
				$query .= " WHERE ";
			};
			$query .= " " . $_GET["manual{$y}_field"] . " " . $_GET["manual{$y}_range"] . " '" . $_GET["manual{$y}_value"] . "' ";
			$x++;
		};
	};
	
	if (!isset($_GET["offset"])) {$_GET["offset"] = 0;};

	// Sort results
/*
	$y = 0;
	for ($x = 1; $x <= $orderby_num; $x++) {
		if ($_GET["sort1"] != "") {$y++};
	};
	if ($y > 0) {$query .= " ORDER BY ";};
*/

	if ($_GET["sort1"] != "" || $_GET["sort2"] != "" || $_GET["sort3"] != "") {$query .= " ORDER BY ";};
	if ($_GET["sort1"] != "") {$query .= " " . $_GET["sort1"] . " " . $_GET["sortdir1"];};
//	if ($_GET["sort2"] != "") {$query .= " " . $_GET["sort2"] . " " . $_GET["sortdir2"];};
//	if ($_GET["sort3"] != "") {$query .= " " . $_GET["sort3"] . " " . $_GET["sortdir3"];};

	$query .= " LIMIT " . $_GET["limit"] . " OFFSET " . $_GET["offset"] . " ";
	echo $query;

	global $db;
	$result = mysql_query($query,$db);

	if ($db_row = mysql_fetch_array($result)) {
		$x = 1 + $_GET["offset"];
		echo "<a name=\"results\">
<table border=\"1\">
	<tr>
		<th>Result</th>
		<th>ID</th>
		<th>Name</th>
	</tr>\n";
		do {
			$ItemLink = "./?id=" . $db_row["id"];
			echo "\t<tr>
		<td align=\"right\">" . $x++ . "</td>
		<td>" . $db_row["id"] . "</td>
		<td>
			<a href=\"" . $ItemLink . "\" target=\"Item\" onclick=\"OpenItem('" . $ItemLink . "')\">" . $db_row["Name"] . "</a>
		</td>
		<td>
			<a href=\"edit.php?id=" . $db_row["id"] . "\">Edit</a>
		</td>
	</tr>\n";
		} while ($db_row = mysql_fetch_array($result));
		echo "</table>";
	} else {
		echo "No results.";
	};

};

?>

</body>

</html>